package com.nebor.product.application.controller;


import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.nebor.product.application.convert.ProductCategoryDtoConverter;
import com.nebor.product.application.dto.ProductCategoryDTO;
import com.nebor.product.common.entity.Result;
import com.nebor.product.domain.bo.ProductCategoryBO;
import com.nebor.product.domain.service.ProductCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/product/category")
@Slf4j
public class ProductCategoryController {

    @Resource
    private ProductCategoryDomainService productCategoryDomainService;


    /**
     * 新增商品分类
     * @param productCategoryDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody ProductCategoryDTO productCategoryDTO) {
        try{
            if(log.isInfoEnabled()){
                log.info("ProductCategoryController.add.dto:{}", JSON.toJSONString(productCategoryDTO));
            }
            //先做log
            Preconditions.checkNotNull(productCategoryDTO.getCategoryType(),"分类类型不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(productCategoryDTO.getCategoryName()),"分类名称不能为空");
            Preconditions.checkNotNull(productCategoryDTO.getParentId(),"分类父id不能为空");
            //做check
            ProductCategoryBO productCategoryBO = ProductCategoryDtoConverter.INSTANCE.convertDtoToBo(productCategoryDTO);
            //转类型
            productCategoryDomainService.add(productCategoryBO);
            //调上一层服务
            return Result.ok(true);
        }catch (Exception e){
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(),e);
            return Result.fail("新增分类失败");
        }
    }
    /**
     * 删除商品分类
     * @param productCategoryDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody ProductCategoryDTO productCategoryDTO) {
        try{
            if(log.isInfoEnabled()){
                log.info("ProductCategoryController.delete.dto:{}", JSON.toJSONString(productCategoryDTO));
            }
            //先做log

            Preconditions.checkNotNull(productCategoryDTO.getId(),"分类id不能为空");
            //做check
            ProductCategoryBO productCategoryBO = ProductCategoryDtoConverter.INSTANCE.convertDtoToBo(productCategoryDTO);
            //转类型
            productCategoryDomainService.delete(productCategoryBO);
            //调上一层服务
            return Result.ok(true);
        }catch (Exception e){
            log.error("SubjectCategoryController.delete.error:{}", e.getMessage(),e);
            return Result.fail("删除分类失败");
        }
    }
    /**
     * 更新商品分类
     * @param productCategoryDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody ProductCategoryDTO productCategoryDTO) {
        try{
            if(log.isInfoEnabled()){
                log.info("ProductCategoryController.update.dto:{}", JSON.toJSONString(productCategoryDTO));
            }
            //先做log

            Preconditions.checkNotNull(productCategoryDTO.getId(),"分类id不能为空");
            //做check
            ProductCategoryBO productCategoryBO = ProductCategoryDtoConverter.INSTANCE.convertDtoToBo(productCategoryDTO);
            //转类型
            productCategoryDomainService.update(productCategoryBO);
            //调上一层服务
            return Result.ok(true);
        }catch (Exception e){
            log.error("ProductCategoryController.update.error:{}", e.getMessage(),e);
            return Result.fail("更新分类失败");
        }
    }
    /**
     * 查询商品分类
     * @param productCategoryDTO
     * @return
     */
    @PostMapping("/queryPrimaryCategory")
    public Result<List<ProductCategoryDTO>> queryPrimaryCategory(@RequestBody ProductCategoryDTO productCategoryDTO) {
        try{
            ProductCategoryBO productCategoryBO = ProductCategoryDtoConverter.INSTANCE.
                    convertDtoToBo(productCategoryDTO);
            List<ProductCategoryBO> productCategoryBOList = productCategoryDomainService.queryCategory(productCategoryBO);
            List<ProductCategoryDTO> productCategoryDTOList = ProductCategoryDtoConverter.INSTANCE.
                    convertBoToCategoryDTOList(productCategoryBOList);
            return Result.ok(productCategoryDTOList);
        } catch (Exception e) {
            log.error("ProductCategoryController.update.error:{}", e.getMessage(),e);
            return Result.fail("查询一级分类失败");
        }
    }

    /**
     * 查询分类下分类
     * @param productCategoryDTO
     * @return
     */
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<ProductCategoryDTO>> queryCategoryByPrimary(@RequestBody ProductCategoryDTO productCategoryDTO) {
        try{
            if(log.isInfoEnabled()){
                log.info("ProductCategoryController.queryCategoryByPrimary.dto:{}",
                        JSON.toJSONString(productCategoryDTO));
            }
            Preconditions.checkNotNull(productCategoryDTO.getParentId(),"分类父id不能为空");
            ProductCategoryBO productCategoryBO = ProductCategoryDtoConverter.INSTANCE.
                    convertDtoToBo(productCategoryDTO);
            List<ProductCategoryBO> productCategoryBOList = productCategoryDomainService.queryCategory(productCategoryBO);
            List<ProductCategoryDTO> productCategoryDTOList = ProductCategoryDtoConverter.INSTANCE.
                    convertBoToCategoryDTOList(productCategoryBOList);
            return Result.ok(productCategoryDTOList);
        } catch (Exception e) {
            log.error("ProductCategoryController.queryCategoryByPrimary.error:{}", e.getMessage(),e);
            return Result.fail("查询二级分类失败");
        }
    }
}
